package com.avic.rong.search.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.adapter.SearchViewAdapter.SearchAdapter;
import com.avic.rong.bean.SearchViewBean.Bean;
import com.avic.rong.wedgit.customView.SearchView;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerDownTimeRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerStateRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerUpTimeRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerChuBanSheRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerDateRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerFenLeiRvAdapter;
import com.avic.rong.goodsSort.bean.GoodsSortDrawerBean;
import com.avic.rong.search.MessageEvent;
import com.avic.rong.search.adapter.searchHomeDrawerAdapters.SearchHomeChuBanSheShrinkRvAdapter;
import com.avic.rong.search.adapter.searchHomeDrawerAdapters.SearchHomeQuanBuShrinkRvAdapter;
import com.avic.rong.search.adapter.searchHomeDrawerAdapters.SearchHomeTuShuShrinkRvAdapter;
import com.avic.rong.search.api.SearchTsAttributeApi;
import com.avic.rong.search.bean.SearchTsAttributeBean;
import com.avic.rong.search.db.User;
import com.avic.rong.search.db.UserDao;
import com.avic.rong.search.fragment.SearchResultFragment;
import com.avic.rong.search.fragment.SearchViewHomeFragment;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SearchViewActivity extends BaseActivity implements SearchView.SearchViewListener, View.OnClickListener, GoodsSortBzDrawerUpTimeRvAdapter.GoodsSortBzDrawerUpTimeListener, GoodsSortBzDrawerStateRvAdapter.GoodsSortBzDrawerStateListener, GoodsSortBzDrawerDownTimeRvAdapter.GoodsSortBzDrawerDownTimeListener
        , GoodsSortTsDrawerChuBanSheRvAdapter.GoodsSortTsDrawerChuBanSheListener,
        GoodsSortTsDrawerDateRvAdapter.GoodsSortTsDrawerDateListener, GoodsSortTsDrawerFenLeiRvAdapter.GoodsSortTsDrawerFenLeiListener{
    @BindView(R.id.main_search_layout)
    SearchView mainSearchLayout;
    @BindView(R.id.search_frameLayout)
    FrameLayout searchFrameLayout;
    @BindView(R.id.goods_sort_bz_drawer_zhankai)
    TextView goodsSortBzDrawerZhankai;
    @BindView(R.id.goods_sort_bz_drawer_topRv)
    RecyclerView goodsSortBzDrawerTopRv;
    @BindView(R.id.goods_sort_bz_drawer_fabu_start)
    TextView goodsSortBzDrawerFabuStart;
    @BindView(R.id.goods_sort_bz_drawer_fabu_end)
    TextView goodsSortBzDrawerFabuEnd;
    @BindView(R.id.goods_sort_bz_drawer_fabuRv)
    RecyclerView goodsSortBzDrawerFabuRv;
    @BindView(R.id.goods_sort_bz_drawer_shishi_start)
    TextView goodsSortBzDrawerShishiStart;
    @BindView(R.id.goods_sort_bz_drawer_shishi_end)
    TextView goodsSortBzDrawerShishiEnd;
    @BindView(R.id.goods_sort_bz_drawer_shishiRv)
    RecyclerView goodsSortBzDrawerShishiRv;
    @BindView(R.id.goods_sort_bz_drawer_qicaoEdit)
    EditText goodsSortBzDrawerQicaoEdit;
    @BindView(R.id.goods_sort_bz_drawer_danweiEdit)
    EditText goodsSortBzDrawerDanweiEdit;
    @BindView(R.id.goods_sort_bz_drawer_Reset)
    TextView goodsSortBzDrawerReset;
    @BindView(R.id.goods_sort_bz_drawer_Sure)
    TextView goodsSortBzDrawerSure;
    @BindView(R.id.goods_sort_bz_drawer)
    ConstraintLayout goodsSortBzDrawer;
    @BindView(R.id.linear_Ts)
    LinearLayout linearTs;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_tv)
    TextView goodsSortTsDrawerFenleiTv;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_rv)
    RecyclerView goodsSortTsDrawerFenleiRv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_tv)
    TextView goodsSortTsDrawerChubansheTv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_rv)
    RecyclerView goodsSortTsDrawerChubansheRv;
    @BindView(R.id.goods_sort_ts_drawer_author_edit)
    EditText goodsSortTsDrawerAuthorEdit;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_start)
    TextView goodsSortTsDrawerChubantimeStart;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_end)
    TextView goodsSortTsDrawerChubantimeEnd;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_rv)
    RecyclerView goodsSortTsDrawerChubantimeRv;
    @BindView(R.id.goods_sort_ts_drawer_Reset)
    TextView goodsSortTsDrawerReset;
    @BindView(R.id.goods_sort_ts_drawer_Sure)
    TextView goodsSortTsDrawerSure;
    @BindView(R.id.goods_sort_ts_drawer)
    ConstraintLayout goodsSortTsDrawer;
    @BindView(R.id.linear_Goods)
    LinearLayout linearGoods;
    @BindView(R.id.searchView_DrawerLayout)
    DrawerLayout searchViewDrawerLayout;

    private GoodsSortBzDrawerUpTimeRvAdapter drawerUptimeAdapter;
    private GoodsSortBzDrawerDownTimeRvAdapter drawerDowntimeAdapter;
    private GoodsSortBzDrawerStateRvAdapter drawerStateAdapter;
    private List<GoodsSortDrawerBean> timeList = new ArrayList<>();
    private List<GoodsSortDrawerBean> timeList1 = new ArrayList<>();
    private List<GoodsSortDrawerBean> stateList = new ArrayList<>();

    private GoodsSortTsDrawerFenLeiRvAdapter tsDrawerFenLeiRvAdapter;
    private GoodsSortTsDrawerChuBanSheRvAdapter tsDrawerChuBanSheRvAdapter;
    private GoodsSortTsDrawerDateRvAdapter tsDrawerDateRvAdapter;
    private List<SearchTsAttributeBean.BookCatBean> tsTsFenLeiDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> timeDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> chubansheDrawerList = new ArrayList();

//    @BindView(R.id.main_lv_search_results)
//    ListView mListView;
    /**
     * 搜索结果列表view
     */
    private ListView lvResults;

    /**
     * 搜索view
     */
    private SearchView searchView;


    /**
     * 热搜框列表adapter
     */
    private ArrayAdapter<String> hintAdapter;

    /**
     * 自动补全列表adapter
     */
    private ArrayAdapter<String> autoCompleteAdapter;

    /**
     * 搜索结果列表adapter
     */
    private SearchAdapter resultAdapter;

    private List<Bean> dbData;

    /**
     * 热搜版数据
     */
    private List<String> hintData;

    /**
     * 搜索过程中自动补全数据
     */
    private List<String> autoCompleteData;

    /**
     * 搜索结果的数据
     */
    private List<Bean> resultData;

    /**
     * 默认提示框显示项的个数
     */
    private static int DEFAULT_HINT_SIZE = 4;

    /**
     * 提示框显示项的个数
     */
    private static int hintSize = DEFAULT_HINT_SIZE;

    /**
     * frameLayout显示状态
     */
    private static int ReMen = 0;
    private static int SouSuo = 1;
    private static int XianShi = 2;
    /**
     * 侧滑view适配器
     */
    private SearchHomeQuanBuShrinkRvAdapter quanBuShrinkRvAdapter;
    private SearchHomeTuShuShrinkRvAdapter tuShuShrinkRvAdapter;
    private SearchHomeChuBanSheShrinkRvAdapter chubanTimeShrinkRvAdapter;
    private SearchHomeChuBanSheShrinkRvAdapter chuBanSheShrinkRvAdapter;

    private static Fragment fragment = new SearchResultFragment();
    private List<SearchTsAttributeBean.BookCatBean> bookCatList;
    private List<String> chubansheList;
    private List<String> chubanTimeList;

    public boolean isShowBuQuanFlag = false;
    public String searchText;

    /**
     * 设置提示框显示项的个数
     */
    public static void setHintSize(int hintSize) {
        SearchViewActivity.hintSize = hintSize;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchview_main);
        ButterKnife.bind(this);
        initData();
        getDrawerData();
        initViews();
        initListener();
        showFragment(SouSuo);
        initAdapter();
    }


    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    protected void initView() {

        User user = new User();
        user.setText("ddd");
        UserDao userDao = (UserDao) MyApplication.getInstance().getDaoSession().getDao(UserDao.class);
        userDao.save(user);


        Logger.i("-------" + userDao.count());

    }

    @Override
    protected void addListener() {

    }

    private void initAdapter() {
        GoodsSortDrawerBean timeBean1 = new GoodsSortDrawerBean("近一个月", "60",false, true);
        GoodsSortDrawerBean timeBean2 = new GoodsSortDrawerBean("近三个月","60", false, true);
        GoodsSortDrawerBean timeBean3 = new GoodsSortDrawerBean("近半年","60",  false, true);
        GoodsSortDrawerBean timeBean4 = new GoodsSortDrawerBean("近一年", "60", false, true);

        timeList.add(timeBean1);
        timeList.add(timeBean2);
        timeList.add(timeBean3);
        timeList.add(timeBean4);

        GoodsSortDrawerBean timeBean5 = new GoodsSortDrawerBean("近一个月", "61", false, true);
        GoodsSortDrawerBean timeBean6 = new GoodsSortDrawerBean("近三个月", "61", false, true);
        GoodsSortDrawerBean timeBean7 = new GoodsSortDrawerBean("近半年", "61", false, true);
        GoodsSortDrawerBean timeBean8 = new GoodsSortDrawerBean("近一年","61",  false, true);

        timeList1.add(timeBean5);
        timeList1.add(timeBean6);
        timeList1.add(timeBean7);
        timeList1.add(timeBean8);

        GoodsSortDrawerBean stateBean1 = new GoodsSortDrawerBean("标准","53",  false, true);
        GoodsSortDrawerBean stateBean2 = new GoodsSortDrawerBean("废止","53",  false, true);
        GoodsSortDrawerBean stateBean3 = new GoodsSortDrawerBean("限制", "53", false, true);

        stateList.add(stateBean1);
        stateList.add(stateBean2);
        stateList.add(stateBean3);

        drawerUptimeAdapter = new GoodsSortBzDrawerUpTimeRvAdapter(this, timeList);
        drawerUptimeAdapter.setGoodsSortBzDrawerUpTimeListener(this);
        drawerStateAdapter = new GoodsSortBzDrawerStateRvAdapter(this, stateList);
        drawerStateAdapter.setGoodsSortBzDrawerStateListener(this);
        drawerDowntimeAdapter = new GoodsSortBzDrawerDownTimeRvAdapter(this, timeList1);
        drawerDowntimeAdapter.setGoodsSortBzDrawerDownTimeListener(this);
        //标准状态
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 3);
        goodsSortBzDrawerTopRv.setLayoutManager(gridLayoutManager2);
        goodsSortBzDrawerTopRv.setAdapter(drawerStateAdapter);
        //发布日期
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        goodsSortBzDrawerFabuRv.setLayoutManager(gridLayoutManager);
        goodsSortBzDrawerFabuRv.setAdapter(drawerUptimeAdapter);
        //实施日期
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 3);
        goodsSortBzDrawerShishiRv.setLayoutManager(gridLayoutManager1);
        goodsSortBzDrawerShishiRv.setAdapter(drawerDowntimeAdapter);

        GridLayoutManager tsFenLeiManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerFenleiRv.setLayoutManager(tsFenLeiManager);
        tsDrawerFenLeiRvAdapter = new GoodsSortTsDrawerFenLeiRvAdapter(this);
        goodsSortTsDrawerFenleiRv.setAdapter(tsDrawerFenLeiRvAdapter);
        tsDrawerFenLeiRvAdapter.setGoodsSortBzDrawerFenLeiListener(this);

        GridLayoutManager tsChuBanSheManager = new GridLayoutManager(this, 2);
        goodsSortTsDrawerChubansheRv.setLayoutManager(tsChuBanSheManager);
        tsDrawerChuBanSheRvAdapter = new GoodsSortTsDrawerChuBanSheRvAdapter(this);
        goodsSortTsDrawerChubansheRv.setAdapter(tsDrawerChuBanSheRvAdapter);
        tsDrawerChuBanSheRvAdapter.setGoodsSortBzDrawerStateListener(this);

        GridLayoutManager tsDateManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerChubantimeRv.setLayoutManager(tsDateManager);
        tsDrawerDateRvAdapter = new GoodsSortTsDrawerDateRvAdapter(this);
        goodsSortTsDrawerChubantimeRv.setAdapter(tsDrawerDateRvAdapter);
        tsDrawerDateRvAdapter.setGoodsSortBzDrawerDateListener(this);

    }

    //获取图书属性数据监听
    private void getDrawerData() {
        SearchTsAttributeApi goodsSortApi = new SearchTsAttributeApi(this, delCartListener);
        goodsSortApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortApi);
    }

    //获取图书属性数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                Log.e("cjx", "图书属性:" + subjects.getData());
                return;
            }
            SearchTsAttributeBean bean = (SearchTsAttributeBean) subjects.getData();
            Log.e("cjx", "图书属性拿到数据");
            tsTsFenLeiDrawerList.addAll(bean.getBookCat());
            int attrId = bean.getAllAttrList().get(0).getAttrId();
            tsDrawerFenLeiRvAdapter.setTsTsFenLeiDrawerList(tsTsFenLeiDrawerList);
            List<SearchTsAttributeBean.AllAttrListBean> allAttrDrawerList = bean.getAllAttrList();
            List<String> attrValuesList = allAttrDrawerList.get(0).getAttrValues();
            for (int i = 0; i < attrValuesList.size(); i++) {
                String s = attrValuesList.get(i);
                GoodsSortDrawerBean goodsSortDrawerBean = new GoodsSortDrawerBean(s, attrId+"",false, true);
                chubansheDrawerList.add(goodsSortDrawerBean);
            }
            tsDrawerChuBanSheRvAdapter.setTsTsFenLeiDrawerList(chubansheDrawerList);
            GoodsSortDrawerBean timeBean1 = new GoodsSortDrawerBean("近一个月",95+"", false, true);
            GoodsSortDrawerBean timeBean2 = new GoodsSortDrawerBean("近三个月", 95+"",false, true);
            GoodsSortDrawerBean timeBean3 = new GoodsSortDrawerBean("近半年",95+"", false, true);
            GoodsSortDrawerBean timeBean4 = new GoodsSortDrawerBean("近一年",95+"", false, true);
            timeDrawerList.add(timeBean1);
            timeDrawerList.add(timeBean2);
            timeDrawerList.add(timeBean3);
            timeDrawerList.add(timeBean4);
            tsDrawerDateRvAdapter.setTsDrawerDateList(timeDrawerList);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    private void initListener() {
        goodsSortBzDrawerFabuStart.setOnClickListener(this);
        goodsSortBzDrawerFabuEnd.setOnClickListener(this);
        goodsSortBzDrawerShishiStart.setOnClickListener(this);
        goodsSortBzDrawerShishiEnd.setOnClickListener(this);

        goodsSortBzDrawerReset.setOnClickListener(this);
        goodsSortBzDrawerSure.setOnClickListener(this);


        goodsSortTsDrawerFenleiTv.setOnClickListener(this);
        goodsSortTsDrawerChubansheTv.setOnClickListener(this);
        goodsSortTsDrawerChubantimeStart.setOnClickListener(this);
        goodsSortTsDrawerChubantimeEnd.setOnClickListener(this);

        goodsSortTsDrawerReset.setOnClickListener(this);
        goodsSortTsDrawerSure.setOnClickListener(this);
        //设置抽屉不可手势划出
        searchViewDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    //打开标准抽屉
    public void openBzDrawer() {
        linearTs.setVisibility(View.VISIBLE);
        linearGoods.setVisibility(View.GONE);
        searchViewDrawerLayout.openDrawer(GravityCompat.END);
    }

    //打开商品抽屉
    public void openGoodsDrawer() {
        linearTs.setVisibility(View.GONE);
        linearGoods.setVisibility(View.VISIBLE);
        searchViewDrawerLayout.openDrawer(GravityCompat.END);
    }

    private void setAdapters() {

    }

    public void showFragment(int index) {
        switch (index) {
            case 0:
                //搜索结果页
                fragment = new SearchResultFragment();
                break;
            case 1:
                //热门、记录界面
                fragment = new SearchViewHomeFragment();
                break;
            case 2:
                break;
        }
        this.getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.search_frameLayout, fragment)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                .commit();
    }

    private void initViews() {
    }

    private void initData() {
        //从数据库获取数据
        getDbData();
        //初始化热搜版数据
        getHintData();
        //初始化自动补全数据
        getAutoCompleteData(null);
        //初始化搜索结果数据
        getResultData(null);
    }

    /**
     * 获取搜索结果data和adapter
     */
    private void getResultData(String text) {
        if (resultData == null) {
            // 初始化
            resultData = new ArrayList<>();
        } else {
            resultData.clear();
            for (int i = 0; i < dbData.size(); i++){
                if (dbData.get(i).getTitle().contains(text.trim())) {
                    resultData.add(dbData.get(i));
                }
            }
        }
        if (resultAdapter == null) {
            resultAdapter = new SearchAdapter(this, resultData, R.layout.item_bean_list);
        } else {
            resultAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取自动补全data 和adapter
     */
    private void getAutoCompleteData(String text) {
        if (autoCompleteData == null) {
            //初始化
            autoCompleteData = new ArrayList<>(hintSize);
        } else {
            // 根据text 获取auto data
            autoCompleteData.clear();
            for (int i = 0, count = 0; i < dbData.size()
                    && count < hintSize; i++) {
                if (dbData.get(i).getTitle().contains(text.trim())) {
                    autoCompleteData.add(dbData.get(i).getTitle());
                    count++;
                }
            }
        }
        if (autoCompleteAdapter == null) {
            autoCompleteAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteData);
        } else {
            autoCompleteAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取热搜版data 和adapter
     */
    private void getHintData() {
        hintData = new ArrayList<>(hintSize);
        for (int i = 1; i <= hintSize; i++) {
            hintData.add("热搜版" + i + "：Android自定义View");
        }
        hintAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, hintData);
    }

    private void getDbData() {
        int size = 100;
        dbData = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            dbData.add(new Bean(R.drawable.icon, "android开发必备技能" + (i + 1), "Android自定义view——自定义搜索view", i * 20 + 2 + ""));
        }
    }

    /**
     * 当搜索框 文本改变时 触发的回调 ,更新自动补全数据
     */
    @Override
    public void onRefreshAutoComplete(String text) {
        //更新数据
        getAutoCompleteData(text);
        isShowBuQuanFlag = true;
        searchText = text;
        EventBus.getDefault().post(new MessageEvent(text));
    }


    /**
     * 点击搜索键时edit text触发的回调
     */
    @Override
    public void onSearch(String text) {
        //更新result数据
        getResultData(text);
        lvResults.setVisibility(View.VISIBLE);
        //第一次获取结果 还未配置适配器
        if (lvResults.getAdapter() == null) {
            //获取搜索数据 设置适配器
            lvResults.setAdapter(resultAdapter);
        } else {
            //更新搜索数据
            resultAdapter.notifyDataSetChanged();
        }
//        DaoSession daoSession = MyApplication.getInstance().getDaoSession();
//
//
//
//        User user = new User(null, text);
//        daoSession.insert(user);
        SQLiteDatabase db = MyApplication.getInstance().getDbHelper();
        ContentValues values = new ContentValues();
        values.put("text", text);
        int num = (int) db.insert("jilutext", null, values);
        Log.e("cjx","db:"+num);
        Toast.makeText(this, "完成搜索", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.searchView_Drawer_QuanBu_More:
                break;
            case R.id.searchView_Drawer_TuShu_More:
                break;
//发布start时间
            case R.id.goods_sort_bz_drawer_fabu_start:
                View view = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop(view);
                for (int i = 0; i < timeList.size(); i++) {
                    timeList.get(i).setClickable(false);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_fabu_end:
                View view1 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop1(view1);
                for (int i = 0; i < timeList.size(); i++) {
                    timeList.get(i).setClickable(false);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_shishi_start:
                View view2 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop2(view2);
                for (int i = 0; i < timeList1.size(); i++) {
                    timeList1.get(i).setClickable(false);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_shishi_end:
                View view3 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop3(view3);
                for (int i = 0; i < timeList1.size(); i++) {
                    timeList1.get(i).setClickable(false);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                break;
            //清空重置
            case R.id.goods_sort_bz_drawer_Reset:
                goodsSortBzDrawerZhankai.setText("全部  ");
                for (int i=0; i<stateList.size();i++){
                    stateList.get(i).setClickable(true);
                    stateList.get(i).setFlag(false);
                }
                goodsSortBzDrawerFabuStart.setText("起始日期");
                goodsSortBzDrawerFabuEnd.setText("截止日期");
                goodsSortBzDrawerShishiStart.setText("起始日期");
                goodsSortBzDrawerShishiEnd.setText("截止日期");
                for (int j=0;j<timeList.size();j++){
                    timeList.get(j).setFlag(false);
                    timeList.get(j).setClickable(true);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                for (int k=0;k<timeList1.size();k++){
                    timeList1.get(k).setFlag(false);
                    timeList1.get(k).setClickable(true);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                goodsSortBzDrawerQicaoEdit.setText("");
                goodsSortBzDrawerDanweiEdit.setText("");
                drawerStateAdapter.notifyDataSetChanged();

                break;
            //确认
            case R.id.goods_sort_bz_drawer_Sure:
                break;
            case R.id.goods_sort_ts_drawer_fenlei_tv:
                if (tsDrawerFenLeiRvAdapter.getItemCount() == 6) {
                    tsDrawerFenLeiRvAdapter.addItemNums(tsTsFenLeiDrawerList.size());
                    tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerFenleiTv.setText("收回  ");
                } else {
                    tsDrawerFenLeiRvAdapter.addItemNums(6);
                    tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerFenleiTv.setText("全部  ");
                }
                tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_chubanshe_tv:
                if (tsDrawerChuBanSheRvAdapter.getItemCount() == 6) {
                    tsDrawerChuBanSheRvAdapter.addItemNums(chubansheDrawerList.size());
                    tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerChubansheTv.setText("收回  ");
                } else {
                    tsDrawerChuBanSheRvAdapter.addItemNums(6);
                    tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                    goodsSortTsDrawerChubansheTv.setText("全部  ");
                }
                tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_chubantime_start:
                View view4 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop4(view4);
                break;
            case R.id.goods_sort_ts_drawer_chubantime_end:
                View view5 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop5(view5);
                break;
            case R.id.goods_sort_ts_drawer_Reset:
                goodsSortTsDrawerFenleiTv.setText("全部  ");
                goodsSortTsDrawerChubansheTv.setText("全部  ");
                goodsSortTsDrawerChubantimeStart.setText("起始日期");
                goodsSortTsDrawerChubantimeEnd.setText("截止日期");
                for (int i=0;i<tsTsFenLeiDrawerList.size();i++){
                    tsTsFenLeiDrawerList.get(i).setSelected(false);
                }
                tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                for (int i=0;i<chubansheDrawerList.size();i++){
                    chubansheDrawerList.get(i).setFlag(false);
                }
                tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                for (int i=0;i<timeDrawerList.size();i++){
                    timeDrawerList.get(i).setFlag(false);
                }
                tsDrawerDateRvAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_ts_drawer_Sure:
                break;
        }
    }

    @Override
    public void onDownTime(int index) {
        for (int i = 0; i < timeList1.size(); i++) {
            if (i == index)
                timeList1.get(i).setFlag(!timeList1.get(i).isFlag());
            else
                timeList1.get(i).setFlag(false);
        }
        if (timeList1.get(index).isFlag()) {
            goodsSortBzDrawerShishiStart.setClickable(false);
            goodsSortBzDrawerShishiEnd.setClickable(false);
        } else {
            goodsSortBzDrawerShishiStart.setClickable(true);
            goodsSortBzDrawerShishiEnd.setClickable(true);
        }
        drawerDowntimeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onStateClick(int index) {
        for (int i = 0; i < stateList.size(); i++) {
            if (i == index) {
                stateList.get(i).setFlag(!stateList.get(i).isFlag());
            } else {
                stateList.get(i).setFlag(false);
            }
        }
        goodsSortBzDrawerZhankai.setText(stateList.get(index).getS() + "  ");
        drawerStateAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUpTimeClick(int index) {
        for (int i = 0; i < timeList.size(); i++) {
            if (i == index)
                timeList.get(i).setFlag(!timeList.get(i).isFlag());
            else
                timeList.get(i).setFlag(false);
        }
        if (timeList.get(index).isFlag()) {
            goodsSortBzDrawerFabuStart.setClickable(false);
            goodsSortBzDrawerFabuEnd.setClickable(false);
        } else {
            goodsSortBzDrawerFabuStart.setClickable(true);
            goodsSortBzDrawerFabuEnd.setClickable(true);
        }
        drawerUptimeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsDrawerChuBanSheSelected(int index) {
        for (int i = 0; i < chubansheDrawerList.size(); i++) {
            chubansheDrawerList.get(index).setFlag(!chubansheDrawerList.get(i).isFlag());
        }
        StringBuffer sb = new StringBuffer();
        for (int j=0;j<chubansheDrawerList.size();j++){
            if (chubansheDrawerList.get(j).isFlag()){
                sb.append(chubansheDrawerList.get(j).getS()+"丶");
            }
        }
        goodsSortTsDrawerChubansheTv.setText(sb.toString());
        tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsDrawerDateSelected(int index) {
        for (int i = 0; i < timeDrawerList.size(); i++) {
            if (i == index) {
                timeDrawerList.get(i).setFlag(!timeDrawerList.get(i).isFlag());
            } else {
                timeDrawerList.get(i).setFlag(false);
            }
        }
        tsDrawerDateRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsFenLeiSelected(int index) {
        for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
            if (i == index) {
                tsTsFenLeiDrawerList.get(i).setSelected(!tsTsFenLeiDrawerList.get(i).isSelected());
            } else {
                tsTsFenLeiDrawerList.get(i).setSelected(false);
            }
        }
        goodsSortTsDrawerFenleiTv.setText(tsTsFenLeiDrawerList.get(index).getCatName() + "  ");
        tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
    }

    //时间选择弹窗
    private void commonPop(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerFabuStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop1(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerFabuEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop2(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerShishiStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop3(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerShishiEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop4(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop5(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
//        WindowManager.LayoutParams lp = getWindow()
//                .getAttributes();
//        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
//        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            // 在dismiss中恢复透明度
//            public void onDismiss() {
//                WindowManager.LayoutParams lp = getWindow()
//                        .getAttributes();
//                lp.alpha = 1f;
//                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//                getWindow().setAttributes(lp);
//            }
//        });
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

}
